//
//  LevelBaseLayer.cpp
//  FruitCrush
//
//  Created by gaoyu on 15/7/26.
//
//

#include "LevelBaseLayer.h"
#include "ShowWinScreen.h"
#include "ShowFailScreen.h"
#include "HeroUsed.h"

#define MATRIX_WIDTH (9)
#define MATRIX_HEIGHT (6)


#define MONSTER_VIEW_XPAD  0
#define MONSTER_VIEW_TOPPAD  0
#define CRUSH_VIEW_X   0
#define CRUSH_VIEW_Y   150

#define PAUSE_BUTTON_X    250
#define PAUSE_BUTTON_Y    500

typedef enum {
    ZBg = 0,
    ZMonsterView,
    ZCrushView,
    ZMenu,
    ZStartView,
    ZEndView,

} Zindex;

typedef enum {
    TagMenu = 1,
    TagPause,
    TagRun,
    
    
} MenuTag;

LevelBaseLayer::LevelBaseLayer()
:m_isPause(false)
,monsterType(0)
,bad_matrix(NULL)
,bad_matrixUp(NULL)
,m_level(0)
,m_bombNum(0)
,bad_matrixProtectCover(NULL)
,jelly_Matrix(NULL)
{
}

LevelBaseLayer::~LevelBaseLayer()
{
    if(bad_matrix){
    	free(bad_matrix);
    }

    if(bad_matrixUp){
    	free(bad_matrixUp);
    }
    if(jelly_Matrix){
    	free(jelly_Matrix);
    }
    if(bad_matrixProtectCover){
    	free(bad_matrixProtectCover);
    }

}

bool LevelBaseLayer::baseInit()
{
    if (!Layer::init()) {
        return false;
    }
    
    bgInit();
    monsterViewInit();
    timeBarViewInit();
    optionMenuViewInit();
    pauseMenuInit();
    startPromptViewInit();
    return true;
}

void LevelBaseLayer::bgInit()
{
    // background
    Size winSize = Director::getInstance()->getWinSize();
    auto background = Sprite::create("background.png");
    background->setAnchorPoint(Point(0, 1));
    background->setPosition(Point(0, winSize.height));
    this->addChild(background,-1);
}

void LevelBaseLayer::startPromptViewInit()
{
    startPromptView = NULL;
    startPromptView = StartPromptView::create();
    startPromptView->setAnchorPoint(Vec2::ZERO);
    startPromptView->setLevel(m_level);
    startPromptView->setCallback(CC_CALLBACK_1(LevelBaseLayer::startPromptViewCallback, this));
    addChild(startPromptView,ZStartView);
    startPromptView->show();
}

void LevelBaseLayer::endPromptViewShow(bool success)
{
    endPromptView = NULL;
    endPromptView = EndPromptView::create();
    endPromptView->setAnchorPoint(Vec2::ZERO);
    addChild(endPromptView,ZEndView);
    endPromptView->show(success);
}

void LevelBaseLayer::startPromptViewCallback(Ref *sender)
{
	crushViewInit();
	scheduleUpdate();
	timeBarView->setPause(false);
	monsterView->setPause(false);
}

void LevelBaseLayer::pauseMenuInit()
{
    auto menuItem = MenuItemImage::create("PauseButton.png", "PauseSelectedButton.png","PauseButton.png", CC_CALLBACK_1(LevelBaseLayer::menuCallback, this));
    menuItem->setAnchorPoint(Vec2::ZERO);
    menuItem->setPosition(Vec2::ZERO);
    menuItem->setTag(TagPause);
    
    auto menuItemRun = MenuItemImage::create("runButton.png", "runButton.png","runButton.png", CC_CALLBACK_1(LevelBaseLayer::menuCallback, this));
    menuItemRun->setAnchorPoint(Vec2::ZERO);
    menuItemRun->setPosition(Vec2::ZERO);
    menuItemRun->setTag(TagRun);
    menuItemRun->setVisible(false);
    
    auto menu = Menu::create(menuItem,menuItemRun,NULL);
    menu->setAnchorPoint(Vec2::ZERO);
    menu->setPosition(Point(PAUSE_BUTTON_X,PAUSE_BUTTON_Y));
    menu->setTag(TagMenu);
    addChild(menu,ZMenu);
}

void LevelBaseLayer::menuCallback(Ref *sender)
{
    Menu* menu = (Menu*)this->getChildByTag(TagMenu);
    MenuItemImage* menuItem = (MenuItemImage*)menu->getChildByTag(TagPause);
    MenuItemImage* menuItemRun = (MenuItemImage*)menu->getChildByTag(TagRun);
    if(m_isPause){
        menuItem->setVisible(true);
        menuItemRun->setVisible(false);
        timeBarView->setPause(false);
        crushView->setPause(false);
        monsterView->setPause(false);
    }else{
        menuItem->setVisible(false);
        menuItemRun->setVisible(true);
        timeBarView->setPause(true);
        crushView->setPause(true);
        monsterView->setPause(true);
    }
    m_isPause = !m_isPause;
    
}

void LevelBaseLayer::monsterViewInit()
{
    Size size = Director::getInstance()->getWinSize();
    monsterView = MonsterView::create(monsterType);
    monsterView->setPosition(Point(MONSTER_VIEW_XPAD,size.height - MONSTER_VIEW_TOPPAD - MonsterView::getSize().height));
    monsterView->setCallback(CC_CALLBACK_1(LevelBaseLayer::monsterViewCallback, this));
    monsterView->setBossAttackCallback(CC_CALLBACK_1(LevelBaseLayer::monsterViewBossAttackCallbackFun, this));
    addChild(monsterView,ZMonsterView);
    monsterView->setPause(true);
}

void LevelBaseLayer::timeBarViewInit()
{
    Size size = Director::getInstance()->getWinSize();
    timeBarView = TimeBarView::create(HeroUsed::getInstance()->getHeroHealth());
    timeBarView->setPosition(Point((size.width - TIME_BAR_VIEW_WIDTH)/2,
                                   TIME_BAR_YPAD));
    timeBarView->setCallback(CC_CALLBACK_1(LevelBaseLayer::timeBarViewCallback, this));
    addChild(timeBarView,ZMenu);
    timeBarView->setPause(true);
}

void LevelBaseLayer::crushViewInit()
{
    crushView = CrushView::create();
    crushView->setAnchorPoint(Vec2::ZERO);
    crushView->setPosition(Point(CRUSH_VIEW_X,CRUSH_VIEW_Y));
    if(bad_matrixBlank){
       	crushView->initMatrixBlank(bad_matrixBlank);
    }
    crushView->initMatrix(jelly_Matrix);
    if(bad_matrix){
    	crushView->initMatrixBad(bad_matrix);
    }
    if(bad_matrixUp){
    	crushView->initMatrixBadUp(bad_matrixUp);
    }
    if(bad_matrixProtectCover){
    	crushView->initMatrixProtectCover(bad_matrixProtectCover);
    }

    crushView->setBombNum(m_bombNum);
    crushView->setCallback(CC_CALLBACK_1(LevelBaseLayer::crushCallback, this));
    crushView->setBadCallback(CC_CALLBACK_1(LevelBaseLayer::crushBadStateCallback, this));
    crushView->setCoverCallback(CC_CALLBACK_1(LevelBaseLayer::crushCoverStateCallback, this));
    addChild(crushView,ZCrushView);
    
}

void LevelBaseLayer::crushCallback(Ref *sender)
{
    CrushView *view = (CrushView*)sender;
    if(view->getCrushType() == HealthAttack){
        timeBarView->changeTime(2);
    }else{
    	int doubleNum = 1;
    	if(view->getCombosNum()>1){
    		doubleNum = 2;
    	}
    	MonsterBeAttackRecorder *recorder = MonsterBeAttackRecorder::create(view->getCrushType(),doubleNum);
        monsterView->beAttacked(recorder);
    }
    
}

void LevelBaseLayer::crushBadStateCallback(Ref *sender)
{
    CrushView *view = (CrushView*)sender;
    monsterView->beHealthed(view->getSuckNum());
}

void LevelBaseLayer::crushCoverStateCallback(Ref *sender)
{
	monsterView->destoryMonsterProtectCover();
}

void LevelBaseLayer::monsterViewCallback(Ref *sender)
{
    timeBarView->recordTime();
//    Director::getInstance()->replaceScene(ShowWinScreen::createScene());
    monsterView->unscheduleUpdate();
    timeBarView->unscheduleUpdate();
    optionMenuView->unscheduleUpdate();
    crushView->unscheduleUpdate();
    			endPromptViewShow(true);
}

void LevelBaseLayer::monsterViewBossAttackCallbackFun(Ref *sender)
{
	crushView->bossCreateJellys();
}

void LevelBaseLayer::timeBarViewCallback(Ref *sender)
{
 //   Director::getInstance()->replaceScene(ShowFailScreen::createScene());
	monsterView->unscheduleUpdate();
	    timeBarView->unscheduleUpdate();
	    optionMenuView->unscheduleUpdate();
	crushView->unscheduleUpdate();
			endPromptViewShow(false);
}

void LevelBaseLayer::optionMenuViewInit()
{
    optionMenuView = OptionMenuView::create();
    optionMenuView->setPosition(Vec2::ZERO);
    addChild(optionMenuView,ZMenu);
}
